package cn.xyf.mzmy.server.controller.user;

import cn.xyf.mzmy.common.idempotent.core.annotation.Idempotent;
import cn.xyf.mzmy.common.pojo.Result;
import cn.xyf.mzmy.common.ratelimiter.core.annotation.RateLimiter;
import cn.xyf.mzmy.server.dal.dataobject.user.User;
import cn.xyf.mzmy.server.mq.message.DemoMessage;
import cn.xyf.mzmy.server.mq.producer.DemoProducer;
import cn.xyf.mzmy.server.service.user.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xyf
 * @since 2025-07-23
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private DemoProducer demoProducer;

    @PostMapping("/test")
    @RateLimiter(count = 5,time = 1,timeUnit = TimeUnit.MINUTES)     // 每1分钟只能操作5次
    public Result<String> test(){
        DemoMessage demoMessage = new DemoMessage();
        demoMessage.setAge(23);
        demoMessage.setName("xyf");
        demoProducer.sendDelayMessage(demoMessage);
        return Result.ok("发送成功");
    }

    @GetMapping("/{id}")
    @Idempotent(timeout = 10,timeUnit = TimeUnit.SECONDS)       // 每10秒钟，所有用户只能操作一次
    public Result<User> queryById(@PathVariable("id") Long id){
        return Result.ok(userService.queryById(id));
    }

}
